// components/showmodel/showmodel.js
import {initCustomerAuthInfo} from "../../utils/publicMethods";
import {downloadFile} from "../../utils/wxapi";
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        icon:{
            type: String,
            value: ''
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        imagesUrl:getApp().globalData.imagesUrl,
        showModalStatus:false,
        avatarUrl:"",
        flag:true,
        username:"",
        icon: "",
        isLoading: false,
        loadingText:""
    },

    /**
     * 组件的方法列表
     */
    methods: {
        //显示对话框
        showModel: async function () {
            if(this.data.avatarUrl == ""){
                let userInfo = await initCustomerAuthInfo();
                if(userInfo.data.retCode == '0000'){
                    let tempFilePath = await downloadFile(userInfo.data.data.headIcon);
                    this.setData({
                        avatarUrl:tempFilePath,
                        username:userInfo.data.data.nickName
                    })
                }
            }
            this.data.flag = true;
            // 显示遮罩层
            var animation = wx.createAnimation({
                duration: 200,
                timingFunction: "linear",
                delay: 0
            })
            this.animation = animation
            animation.translateY(300).step()
            this.setData({
                animationData: animation.export(),
                showModalStatus: true
            })
            setTimeout(() => {
                animation.translateY(0).step()
                this.setData({
                    animationData: animation.export()
                })
            }, 200)
        },
        //隐藏对话框
        hideModal: function () {
            // 隐藏遮罩层
            var animation = wx.createAnimation({
                duration: 200,
                timingFunction: "linear",
                delay: 0
            })
            this.animation = animation
            animation.translateY(300).step()
            this.setData({
                animationData: animation.export(),
            })
            setTimeout(() => {
                animation.translateY(0).step()
                this.setData({
                    animationData: animation.export(),
                    showModalStatus: false
                })
                if(this.data.flag){
                    this.triggerEvent('hideAuthModal')
                }
            }, 200)
        },
         //获取用户头像
        onChooseAvatar(e){
            const { avatarUrl } = e.detail 
            this.setData({
                avatarUrl
            })
        },
        authNameAndPhoto(e){
            let that = this;
            this.setData({
                isLoading: true,
                icon:this.properties.icon,
                loadingText: "正在加载中..."
            })
            this.data.flag = false;//配合hideModal方法使用
            this.hideModal();
            let avatarUrl = this.data.avatarUrl;
            let userName = e.detail.value.username;
            wx.login({
                success: (res) => {
                     let code = res.code;
                     if(userName == "" || userName == null){
                        userName = "";
                     }
                     wx.uploadFile({
                         url: getApp().globalData.requestUrl+'authUserImage',
                         filePath: avatarUrl,
                         name: 'headIcon',
                         header: {
                             "content-type": "multipart/form-data"
                         },
                         formData: {
                             'jsCode': code,
                             'nickName':encodeURI(userName),
                             'userId': wx.getStorageSync('userId')
                         },
                         success: function (res) {
                            that.setData({
                                isLoading:false
                            })
                            that.triggerEvent('authNameAndPhoto', true)
                         },
                         fail: function (res) {
                            that.setData({
                                isLoading:false
                            })
                            that.triggerEvent('authNameAndPhoto', false)
                            console.log("上传图片失败",res);
                         }
                     })
                }
            })
            
        }
    }
})
